<template>
  <div class="p-2">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" :label-col="labelCol" :model="queryParam" :wrapper-col="wrapperCol" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :lg="6">
            <a-form-item name="processDefinitionKey">
              <template #label><span title="流程key">流程key</span></template>
              <a-input v-model:value="queryParam.processDefinitionKey" placeholder="请输入流程key"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item name="category">
              <template #label><span title="流程类别">流程类别</span></template>
              <j-dict-select-tag v-model:value="queryParam.category" dictCode="act_model_type" placeholder="请选择流程类别" />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :lg="6">
              <a-form-item name="version">
                <template #label><span title="流程版本">流程版本</span></template>
                <a-input v-model:value="queryParam.version" placeholder="请输入流程版本"></a-input>
              </a-form-item>
            </a-col>
            <a-col :lg="6">
              <a-form-item name="suspended">
                <template #label><span title="是否已暂停">状态</span></template>
                <j-dict-select-tag v-model:value="queryParam.suspended" dictCode="proccesDefState" placeholder="请选择" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <span class="table-page-search-submitButtons" style="float: left; overflow: hidden">
              <a-col :lg="6">
                <a-button preIcon="ant-design:search-outlined" type="primary" @click="searchQuery">查询</a-button>
                <a-button preIcon="ant-design:reload-outlined" style="margin-left: 8px" type="primary" @click="searchReset">重置</a-button>
                <a style="margin-left: 8px" @click="toggleSearchStatus = !toggleSearchStatus">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <Icon :icon="toggleSearchStatus ? 'ant-design:up-outlined' : 'ant-design:down-outlined'" />
                </a>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable :rowSelection="rowSelection" @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <!-- <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增部署</a-button> -->
      </template>
      <!-- 实例挂起状态 -->
      <template #instanceState="{ text }">
        <a-tag v-if="!text" color="green">激活</a-tag>
        <a-tag v-if="text" color="pink">挂起</a-tag>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <template v-slot:bodyCell="{ column, record, index, text }"> </template>
    </BasicTable>

    <ActProcessInstanceDetail @register="registerDetailModal" />
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue'
  import { BasicTable, TableAction } from '/@/components/Table'
  import { useListPage } from '/@/hooks/system/useListPage'
  import { columns } from './ActProcessInst.data'
  import { deleteOne, endOne, listPage, updateState } from './ActProcessInst.api'
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue'
  import { useUserStore } from '/@/store/modules/user'
  import { useModal } from '@/components/Modal'
  import ActProcessInstanceDetail from './components/ActProcessInstanceDetail.vue'

  const formRef = ref()
  const queryParam = reactive<any>({})
  const toggleSearchStatus = ref<boolean>(false)
  const userStore = useUserStore()
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '流程部署',
      api: listPage,
      columns,
      canResize: false,
      useSearchForm: false,
      actionColumn: {
        width: 280,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam)
      },
    },
  })
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext
  const labelCol = reactive({
    xs: 24,
    sm: 4,
    xl: 6,
    xxl: 4,
  })
  const wrapperCol = reactive({
    xs: 24,
    sm: 20,
  })

  //注册实例详情model
  const [registerDetailModal, { openModal: openDetailModal }] = useModal()

  /**
   * 激活|挂起事件
   */
  function handleUpdateState(record: Recordable) {
    const suspended: boolean = !record.suspended
    updateState({ procInstId: record.id, suspended: suspended }, handleSuccess)
  }

  /**
   * 流程详情
   */
  function handleProcessProgress(record: Recordable) {
    openDetailModal(true, {
      instanceId: record.id,
      isUpdate: false,
      showFooter: false,
    })
  }

  /**
   * 删除事件
   */
  async function handleDelete(record: Recordable) {
    await deleteOne({ procInstId: record.id }, handleSuccess)
  }

  /**
   * 结束事件
   */
  async function handleEnd(record: Recordable) {
    await endOne({ procInstId: record.id }, handleSuccess)
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    ;(selectedRowKeys.value = []) && reload()
  }

  /**
   * 操作栏
   */
  function getTableAction(record: Recordable) {
    return [
      {
        label: record.suspended ? '激活' : '挂起',
        popConfirm: {
          title: '是否确认' + (record.suspended ? '激活' : '挂起'),
          confirm: handleUpdateState.bind(null, record),
          placement: 'topLeft',
        },
      },
      {
        label: '流程详情',
        onClick: handleProcessProgress.bind(null, record),
      },
    ]
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record: Recordable) {
    return [
      {
        label: '转办',
      },
      {
        label: '委派',
      },
      {
        label: '跳转',
      },
      {
        label: '结束',
        popConfirm: {
          title: '是否确认结束',
          confirm: handleEnd.bind(null, record),
          placement: 'topLeft',
        },
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
          placement: 'topLeft',
        },
      },
    ]
  }

  /**
   * 查询
   */
  function searchQuery() {
    reload()
  }

  /**
   * 重置
   */
  function searchReset() {
    formRef.value.resetFields()
    selectedRowKeys.value = []
    //刷新数据
    reload()
  }
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    padding: 0;

    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }

    .query-group-cust {
      min-width: 100px !important;
    }

    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }
</style>
